<?php
namespace common\utils;

use yii\captcha\CaptchaAction;
use Yii;
/**
 */
class CodeImgGenerate extends CaptchaAction {

    private $verifycode;

    public function __construct()
    {
        $this->init();
        // 更多api请访问yii\captcha\CaptchaAction类文档
        // 这里可以初始化默认样式
        $this->maxLength = 4;            // 最大显示个数
        $this->minLength = 4;            // 最少显示个数
        $this->backColor = 0x000000;     // 背景颜色
        $this->foreColor = 0x00ff00;     // 字体颜色
        $this->width = 80;               // 宽度
        $this->height = 45;              // 高度
    }

    /**
     * [返回图片二进制]
     * @return [type] [description]
     */
    public function inline()
    {
        $imgCode = $this->generateVerifyNumberCode();
//        $imgCode = $this->generateVerifyCode();
        $imgCode = strtolower($imgCode);
        $hash = password_hash($imgCode, PASSWORD_BCRYPT, ['cost' => 10]);
        $generator = [
            'value' => $imgCode,
            'key' => $hash,
        ];
        Yii::$app->cache->set("captcha_".$imgCode,$generator,1800);
        return $this->renderImage($imgCode);
    }

    /**
     * [检验图片验证码]
     * @return [type] [description]
     */
    public function check($code = '')
    {
        $code = strtolower(trim($code));
        $name = 'captcha_' . $code;
        if (!Yii::$app->cache->exists($name) || !($generator = Yii::$app->cache->get($name))) {
            return false;
        }
        $key = $generator['key'] ?? '';
        $res = password_verify($code, $key);

        if ($res) {
            Yii::$app->cache->delete($name);
        }

        return $res;
    }


    /**
     * [返回图片验证码]
     * @return [type] [description]
     */
    public function getPhrase()
    {
        if($this->verifycode){
            return $this->verifycode;
        }else{
            return $this->verifycode = $this->generateVerifyNumberCode();
        }
    }

    /**
     * [ 返回随机颜色 ]
     * @param  integer $type [description]
     * @return [type]        [description]
     */
    public static function captchaColor($type = 1)
    {
        if(!in_array($type,[1,2])) $type=1;
        if($type == 1) {
            // 背景颜色
            $bg_color_arr = ['15595519','16316664'];
            $bg = $bg_color_arr[array_rand($bg_color_arr)];
            return (int) '0x'.$bg;
        } else {
            // 字体颜色
            $text_color_arr = ['12326852','2185586'];
            $tc = $text_color_arr[array_rand($text_color_arr)];
            return (int) '0x'.$tc;
        }
    }

    /**
     * Generates a new verification code.
     * @return string the generated verification code
     */
    protected function generateVerifyNumberCode()
    {
        if ($this->minLength > $this->maxLength) {
            $this->maxLength = $this->minLength;
        }
        if ($this->minLength < 3) {
            $this->minLength = 3;
        }
        if ($this->maxLength > 20) {
            $this->maxLength = 20;
        }

        $length = random_int($this->minLength, $this->maxLength);

        $letters = '0123456789';
        $vowels = '0123456789';
        $code = '';
        for ($i = 0; $i <= $length; ++$i) {
            if ($i % 2 && random_int(0, 10) > 2 || !($i % 2) && random_int(0, 10) > 9) {
                $code .= $vowels[random_int(0, 4)];
            } else {
                $code .= $letters[random_int(0, 20)];
            }
        }
        $code = mt_rand(1000,9999);

        return $code;
    }
}